package com.nulldev.util.web.HttpClient.util;

import java.io.InputStream;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.JVM.JVM;
import com.nulldev.util.io.FastIO.FastIO;
import com.nulldev.util.io.file.FileUtil;
import com.nulldev.util.networking.misc.TLSUtil;
import com.nulldev.util.web.HttpClient.HttpClient;
import com.nulldev.util.web.HttpClient.HttpRequest;
import com.nulldev.util.web.HttpClient.HttpResponse;

public class HttpClientSpeedTest {

	public static void main(String[] args) throws Exception {
		Arguments.setup(args);
		JVM.println("TLS Implemnentation: " + TLSUtil.getTLSImplementation());
		final HttpRequest<InputStream> rq = HttpClient.getStream().url("https://sample-videos.com/zip/10mb.zip");
		final HttpResponse<InputStream> sf = rq.create();
		final InputStream ois = sf.data();
		int r = 0, read = 0;
		final long len = sf.length();
		JVM.println("=> Content-Length: " + len);
		JVM.println("=> available (pre): " + ois.available());
		final byte[] data = new byte[8192];
		final long s = System.currentTimeMillis();
		while ((r = ois.read(data)) != -1 && read != len) {
			read += r;
			JVM.println("read: " + read + ", r: " + r);
			if (read == len)
				break;
		}
		final long e = System.currentTimeMillis();
		JVM.println("=> Downloaded.");
		if (read < 1024) {
			JVM.println(new String(data));
		}
		sf.close();
		JVM.println("=> Total read: " + read + " (" + FileUtil.convertToStringRepresentation(read) + ")");
		JVM.println("=> Time taken: " + (e - s) + " ms");
		FastIO.get().flush();
		JVM.exit();
	}

}
